//
//  Emoticon.swift
//  表情键盘
//
//  Created by zhangguo on 15/10/18.
//  Copyright © 2015年 zhangguo. All rights reserved.

import UIKit
//表情包模型,是从emoticon中加载的
class EmoticonsPackage: NSObject {
    
    var id: String
    var groupName: String = ""
    var emoticons: [Emoticon]?
    
    init(id:String,groupName:String = "") {
        //实例化
        self.id = id
        self.groupName = groupName

    }
    
    //加载所有的表情包
    class func packages() ->[EmoticonsPackage] {
        
        //1加载路径
        let path = bundlePath.stringByAppendingString("/emoticons.plist")
        
        //2加载plist
        let dict = NSDictionary(contentsOfFile: path)!
        let array = dict["packages"] as! [[String:AnyObject]]
        
        var arrayM = [EmoticonsPackage]()
        //插入最近分组
        let packages = EmoticonsPackage.init(id: "", groupName: "最近使用").appendEmptyEmotions()
        arrayM.append(packages)
        //3循环遍历数组
      
        for d in array {
            let id = d["id"] as! String
            //链式相应
            let package = EmoticonsPackage(id: id).loadEmoticons().appendEmptyEmotions()
 
            arrayM.append(package)
          
        }
       return arrayM
        
    }

    func loadEmoticons() -> Self {
        //获取路径
    let path = EmoticonsPackage.bundlePath.stringByAppendingString("/"+id).stringByAppendingString("/info.plist")
        
         let dict = NSDictionary(contentsOfFile: path)!
        
    
        groupName = dict["group_name_cn"] as! String
        let array = dict["emoticons"] as? [[String:String]]
        
//        遍历数组
        emoticons = [Emoticon]()
         var index = 0
        for d in array! {
           
            
            emoticons?.append(Emoticon(id: id, dict: d))
            index++
            if index == 20{
               emoticons?.append(Emoticon(remove: true))
                index = 0
            }
            
        }
        
        return self
    }
    
    //追加空白按钮
    func appendEmptyEmotions() -> Self {
        //判断表情数组是否存在
        if emoticons  == nil {
            emoticons = [Emoticon]()
        }
        
        //判断是否是整页，如果不是，追加到20个空白按钮，最后一个放删除按钮
        let cont = (emoticons?.count)! % 21
        print("已经有\(cont)表情")
        
        //等于0说明是空页
        if cont > 0 || cont == 0 {
        for _ in cont ..< 20  {
            emoticons?.append(Emoticon(remove: false))
        }
            //在最后添加删除按钮
            emoticons?.append(Emoticon(remove: true))
    }

        return self
    }
    
    //加载表情包路径
    static  let bundlePath = NSBundle.mainBundle().bundlePath.stringByAppendingString("/Emoticons.bundle")
    
    override var description: String {
        return "\(id)\(groupName)\(emoticons)"
    }
}


//表情模型
class Emoticon: NSObject {
    var id: String?
    //f发送给服务器的表情文字
    var chs: String?
    //本地显示的图片文件名
    var png: String?
    
    //图片完整路径
    var imagePath: String {
        //判断是否是 图片 表情
        if chs == nil {
            return ""
        }
        
       return EmoticonsPackage.bundlePath.stringByAppendingString("/"+id!).stringByAppendingString("/"+png!)
    }
    
    //emoji的代码
    var code:String? {
        didSet{
            //扫描
            let scanner = NSScanner(string: code!)
            var value: UInt32  = 0
            scanner.scanHexInt(&value)
           
            emoji = String(Character(UnicodeScalar(value)))
            
        }
    }
    
    //emoji的字符串
    var emoji:String?
    
    //删除标记
    var removeEmoticon = false
    
    init(remove:Bool){
       
        removeEmoticon = remove
        super.init()
    }
    
    init(id:String,dict: [String: String]) {
        self.id = id
        
        super.init()
        
        // 会根据字典中的值对顺序调用 setValuesForKey
        setValuesForKeysWithDictionary(dict)
    }

    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override var description: String {
        
        return "\(chs)\(png)\(code)\(removeEmoticon)"
    }
    
}


